package com.demomaster.quickcompat.widget.popup;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.demomaster.quickcompat.R;
import com.demomaster.quickcompat.drawable.QDRoundArrowDrawable;
import com.demomaster.quickcompat.util.DisplayUtil;
import com.demomaster.quickcompat.widget.base.QuickGravity;

public class QDTipPopupWindow extends QuickPopupWindow {
    // private Builder builder;
    public int textColor = Color.WHITE;
    public int textSize = 14;
    private String message;
    private int backgroundColor = Color.BLACK;
    private float[] backgroundRadius = new float[8];
    private boolean withArrow;
    private int arrowWidth;
    private int arrowHeight;

    public QDTipPopupWindow(Context context, Builder builder) {
        super(context, builder);
        textColor = builder.textColor;
        textSize = builder.textSize;
        message = builder.message;
        backgroundColor = builder.backgroundColor;
        backgroundRadius = builder.backgroundRadius;
        withArrow = builder.withArrow;
        arrowWidth = builder.arrowWidth;
        arrowHeight = builder.arrowHeight;
        if (arrowHeight == 0 && arrowWidht == 0) {
            withArrow = false;
        }
        if (builder.animationStyleID != -1) {
            setAnimationStyle(builder.animationStyleID);
        }
        setTouchable(builder.mTouchable);
        // setEnterTransition(new );
        init();
    }

    public void showTip(View anchor, QuickGravity quickGravity) {
        this.mAnchor = anchor;
        setDirection(quickGravity);
    }

    private QuickGravity mQuickGravity;
    //private int arrowHeight;
    private int arrowWidht;
    private QDRoundArrowDrawable drawable_bg;

    public void setDirection(QuickGravity quickGravity) {
        this.mQuickGravity = quickGravity;
        //新建一个Drawable对象
        drawable_bg = new QDRoundArrowDrawable(mAnchor, mQuickGravity, getContentView());
        //drawable_bg.setCornerRadii(builder.backgroundRadius);
        drawable_bg.setBackGroundColor(backgroundColor);
        //drawable_bg.setCornerRadius(builder.backgroundRadiu);
        drawable_bg.setCornerRadii(backgroundRadius);
        if (withArrow) {
            arrowWidht = arrowWidth;
        } else {
            arrowHeight = 0;
            arrowWidht = 0;
        }
        drawable_bg.setArrowSize(arrowWidht, arrowHeight);
        getContentView().setBackground(drawable_bg);
        //rootLayout.setBackgroundColor(Color.GREEN);
        show();
    }

    private View mAnchor;

    @Override
    public void showAtLocation(View parent, int gravity, int x, int y) {
        mAnchor = parent;
        super.showAtLocation(parent, gravity, x, y);
    }

    @Override
    public void showAsDropDown(View anchor) {
        mAnchor = anchor;
        setDirection(QuickGravity.BOTTOM);
    }

    private void show() {
        ViewGroup.LayoutParams layoutParams = getContentView().getLayoutParams();
        getContentView().measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        int popupWidth = getContentView().getMeasuredWidth();
        int popupHeight = getContentView().getMeasuredHeight();
        // 设置好参数之后再show
        int[] location = new int[2];
        mAnchor.getLocationOnScreen(location);

        if (mQuickGravity == QuickGravity.BOTTOM || mQuickGravity == QuickGravity.TOP) {
            popupHeight = popupHeight + arrowHeight;
            if (mQuickGravity == QuickGravity.BOTTOM) {
                showAtLocation(mAnchor, QuickGravity.NO_GRAVITY.value(), (location[0] + mAnchor.getMeasuredWidth() / 2) - popupWidth / 2, location[1] + mAnchor.getMeasuredHeight());
            } else {
                showAtLocation(mAnchor, QuickGravity.NO_GRAVITY.value(), (location[0] + mAnchor.getMeasuredWidth() / 2) - popupWidth / 2, location[1] - popupHeight);
            }
        }
        if (mQuickGravity == QuickGravity.LEFT || mQuickGravity == QuickGravity.RIGHT) {
            popupWidth = popupWidth + arrowWidht;
            if (layoutParams != null && popupWidth >= maxWidth) {
                if (mQuickGravity == QuickGravity.LEFT) {
                    layoutParams.width = location[0] - padding1;
                } else {
                    layoutParams.width = location[0] + mAnchor.getMeasuredWidth();
                }
                getContentView().setLayoutParams(layoutParams);
            }
            showAtLocation(mAnchor, QuickGravity.NO_GRAVITY.value(), ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        }
        // 自动调整箭头的位置
        updatePosition();
    }

    @Override
    public void showAsDropDown(View anchor, int xoff, int yoff) {
        mAnchor = anchor;
        super.showAsDropDown(anchor, xoff, yoff);
        if (xoff > 0) {
            setDirection(QuickGravity.LEFT);
        } else {
            setDirection(QuickGravity.RIGHT);
        }
        if (yoff > 0) {
            setDirection(QuickGravity.TOP);
        } else {
            setDirection(QuickGravity.BOTTOM);
        }
    }

    @Override
    public void showAsDropDown(View anchor, int xoff, int yoff, int gravity) {
        mAnchor = anchor;
        super.showAsDropDown(anchor, xoff, yoff, gravity);

        setDirection(QuickGravity.getEnum(gravity));
    }

    private void updatePosition() {
        // 设置好参数之后再show
        int[] location = new int[2];
        mAnchor.getLocationOnScreen(location);
        ViewGroup.LayoutParams layoutParams = getContentView().getLayoutParams();
        if (layoutParams != null) {
            layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        }

        getContentView().measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        int popupWidth = getContentView().getMeasuredWidth();
        int popupHeight = getContentView().getMeasuredHeight();
        //QDLogger.d("getScreenWidth="+DisplayUtil.getScreenWidth(contextWeakReference.get())+"popupWidth="+popupWidth+",popupHeight="+popupHeight);
        int x = 0, y = 0;
        if (mQuickGravity == QuickGravity.BOTTOM || mQuickGravity == QuickGravity.TOP) {
            x = location[0] + mAnchor.getMeasuredWidth() / 2 - popupWidth / 2;
            if (mQuickGravity == QuickGravity.BOTTOM) {
                y = location[1] + mAnchor.getMeasuredHeight();
            } else {
                y = location[1] - popupHeight - arrowHeight;
            }
        }
        if (mQuickGravity == QuickGravity.LEFT || mQuickGravity == QuickGravity.RIGHT) {
            popupWidth = popupWidth + arrowWidht;
            y = location[1] + (popupHeight < mAnchor.getMeasuredHeight() ? (mAnchor.getMeasuredHeight() - popupHeight) / 2 : 0);
            if (mQuickGravity == QuickGravity.LEFT) {
                getContentView().setLayoutParams(layoutParams);
                x = location[0] - popupWidth;
            } else {
                getContentView().setLayoutParams(layoutParams);
                x = location[0] + mAnchor.getWidth();
            }
        }

        update(x, y + arrowHeight, popupWidth, popupHeight);
    }

    private int maxWidth;
    private int maxHeight;
    private int screenHeight;
    private int screenWidth;
    private int padding1;

    private void init() {
        //setTouchable(true);
        setFocusable(true);
        setOutsideTouchable(true);
        setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        padding1 = DisplayUtil.dip2px(getContext(), 30);
        screenHeight = DisplayUtil.getScreenHeight(getContext());
        screenWidth = DisplayUtil.getScreenWidth(getContext());
        maxWidth = screenWidth - padding1 * 2;
        maxHeight = screenHeight - padding1 * 2;
        //内部类调用setContentView先设置isCustomView = false;
        //setContentView(rootLayout);
        TextView textView = getContentView().findViewById(R.id.tv_text);
        if (textView != null) {
            textView.setText(message);
        }
    }

    public static class Builder extends QuickPopupWindow.Builder<Builder> {
        public int textColor = Color.WHITE;
        public int textSize = 14;
        private String message;
        public float[] backgroundRadius = new float[8];
        private boolean withArrow = true;
        private int arrowWidth;
        private int arrowHeight;

        public Builder(Context context) {
            super(context);
            arrowWidth = DisplayUtil.dip2px(context, 8);
            arrowHeight = DisplayUtil.dip2px(context, 8);
            backgroundColor = context.getResources().getColor(R.color.transparent_light_99);
            setBackgroundRadius(DisplayUtil.dip2px(context, 5));
        }

        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }

        public Builder setBackgroundColor(int backgroundColor) {
            this.backgroundColor = backgroundColor;
            return this;
        }

        public Builder setBackgroundRadius(float backgroundRadiu) {
            if (backgroundRadius == null) {
                return this;
            }
            for (int i = 0; i < backgroundRadius.length; i++) {
                this.backgroundRadius[i] = backgroundRadiu;
            }
            return this;
        }

        public Builder setBackgroundRadius(float[] backgroundRadius) {
            this.backgroundRadius = backgroundRadius;
            return this;
        }

        public Builder setTextColor(int textColor) {
            this.textColor = textColor;
            return this;
        }

        public Builder setTextSize(int textSize) {
            this.textSize = textSize;
            return this;
        }

        public Builder setArrowWidth(int arrowWidth) {
            this.arrowWidth = arrowWidth;
            return this;
        }

        public Builder setArrowHeight(int arrowHeight) {
            this.arrowHeight = arrowHeight;
            return this;
        }

        public Builder setWithArrow(boolean withArrow) {
            this.withArrow = withArrow;
            return this;
        }

        public QDTipPopupWindow create() {
            return new QDTipPopupWindow((Context) contextWeakReference.get(), this);
        }
    }
}
